/**
 * Created by hjqi on 2016/2/10.
 */
var express = require('express');
var router = express.Router();
var _getTokenAndUser = require('../lib/common/common').getTokenAndUser;
var _removeExpireToken = require('../lib/common/common').removeExpireToken;
var gitAdpt = require('../lib/git/git_adapter');
var logger = require('../lib/log4js/log4js').logger('gitRepoInfo');
var myRet = require('../lib/common/common').myReturn;

function _processReqRes(req, res,this_handler,getResCall,call_args){

    //get token and user info
    var _token_user_p = new Promise(function (resolve, reject){
        _getTokenAndUser(req,res,function(_ret,resp){
            if(_ret === null){
                logger.debug('getTokenAndUser exception haddpened ');
                reject(resp);
            }
            else
                resolve(_ret);
        });
    });

    //get repolst or taglist
    _token_user_p.then(function(userInfo){
        var _promise = new Promise(function (_resolve, _reject){
            var _callback = function(data,result){
                if(data !== null)
                    _resolve(data);
                else
                    _reject(result);
            }
            call_args.push(userInfo);
            call_args.push(_callback);
            getResCall.apply(this_handler,call_args)
        });

        _promise.then(function(data){
            res.setHeader('Content-Type','application/json');
            res.send(myRet(true,undefined,data));
        });

        _promise.catch(function(resp){
            if(resp.code !== undefined  && resp.code === 401){
                _removeExpireToken(req,function(){
                    _processReqRes(req, res,this_handler,getResCall,call_args);
                    //res.send(resp);
                });
            }else
                res.send(myRet(true,undefined,resp));
        });

    }).catch(function(resp){
        if(resp.statusCode === 301 || resp.statusCode === 302){
            logger.debug('redirect to:'+resp.headers.location);
            res.location(resp.headers.location);
            res.sendStatus(resp.statusCode);
        }else if(resp.code !== undefined  && resp.code === 401){
            _removeExpireToken(req,function(){
                _processReqRes(req, res,this_handler,getResCall,call_args);
            });
        }
        else{
            if(resp instanceof Object){
                logger.debug('other exception:['+JSON.stringify(resp)+']');
                res.status(resp.code);
                res.send(myRet(true,undefined,resp.msg));
            }else{
                logger.debug('other exception:['+resp+']');
                res.send(myRet(true,undefined,resp));
            }

        }
    });
}

router.get('/ReqRepoList',function(req, res, next){

    var param = req.query;
    _processReqRes(req, res,gitAdpt.getGitHandle(param.type),gitAdpt.getGitHandle(param.type).getRepoInfo,[]);

});

router.get('/ReqTagList',function(req, res, next){

    var param = req.query;

    if (param.type == "" || param.type == null) {
        res.status(400).send(myRet(false,"type parameter is required.","type parameter is required."));
        return;
    }
    _processReqRes(req, res, gitAdpt.getGitHandle(param.type), gitAdpt.getGitHandle(param.type).getTagInfo, [param.full_name]);

});

module.exports = router;